<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>财务管理</el-breadcrumb-item>
            <el-breadcrumb-item>借款管理</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-form class="demo-form-inline" ref="queryform_FinanceLoan" :model="SelectProp">
            <el-row :gutter="5" class="row-bg">
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="LoanNumber">
                        <el-input v-model="SelectProp.LoanNumber" placeholder="请输入编号"></el-input>
                    </el-form-item>
                </el-col>

                <el-col :span="3">
                    <el-form-item class="elformitem" prop="Project">
                        <el-select v-model.number="SelectProp.Project" required clearable filterable
                                   placeholder="请选择项目">
                            <el-option v-for="(v,k) in AllProject" :value="v.Id" :data-userid="v.Id"
                                       :label="v.ProjectName" v-text="v.ProjectName"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3">
                    <el-form-item class="elformitem" prop="Borrower">
                        <el-select v-model.number="SelectProp.Borrower" required clearable filterable
                                   placeholder="请选择申请人">
                            <el-option v-for="(v,k) in AllUsers" :value="v.Id" :data-userid="v.Id" :label="v.UserName"
                                       v-text="v.UserName"/>
                        </el-select>
                    </el-form-item>
                </el-col>
                <el-col :span="3.5">
                    <el-date-picker v-model="form.Start"
                                    type="date"
                                    placeholder="开始时间">
                    </el-date-picker>
                </el-col>
                <el-col :span="3">
                    <el-date-picker v-model="form.End"
                                    type="date"
                                    placeholder="结束时间">
                    </el-date-picker>
                </el-col>
                <el-col :span="1">
                    <el-button icon="el-icon-search" style="width:100%" size="medium"
                               @click="_GetFinanceLoanDetail()"></el-button>
                </el-col>

                <el-col :span="1">
                    <el-button icon="el-icon-refresh-right" style="width:100%" size="medium"
                               @click="_GetFinanceLoanDetail_Refresh()"></el-button>
                </el-col>
            </el-row>
        </el-form>

        <el-row class="row-bg">
            <el-col>
                <el-button-group>
                    <el-button class="buttongroup" type="danger" icon="el-icon-delete" @click="DelArray">批量删除
                    </el-button>
                    <el-button class="buttongroup" type="success" icon="el-icon-circle-plus-outline"
                               @click="ShowAddFinanceLoan">添加
                    </el-button>
                </el-button-group>
            </el-col>
        </el-row>

        <el-row style="padding:10px 0px">
            <el-col :span="24">
                <el-table :data="tableData"
                          border
                          @selection-change="SelectionChange_event">
                    <el-table-column header-align="center" type="selection" width="50" align="center"></el-table-column>
                    <el-table-column header-align="center" fixed type="index" label="序号" align="center" width="50"
                                     :index="_indexMethod"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="LoanNumber" label="单据编号"
                                     width="150px"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="Borrower" label="借款人">
                        <template slot-scope="scope">
                            <label>{{_UserById(scope.row.Borrower)}}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="Money" label="借款金额"></el-table-column>
                    <el-table-column header-align="center" align="center" prop="BorrowTime" label="借款时间">
                        <template slot-scope="scope">
                            <label>{{(scope.row.BorrowTime) }}</label>
                        </template>
                    </el-table-column>
                    <el-table-column header-align="center" align="center" prop="Remark" label="备注"></el-table-column>
                    <el-table-column prop="address" header-align="center" align="center" label="操作" fixed="right"
                                         width="200px">
                            <template slot-scope="scope">
                                <el-button type="info" class="BtnSize" size="mini" plain @click="edit(scope.row)">编辑
                                </el-button>
                                <el-button type="danger" class="BtnSize" size="mini" plain @click="del(scope.row)"> 删除
                                </el-button>
                            </template>
                        </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block" style="text-align:right">
                    <el-pagination @size-change="_GetFinanceLoanDetail"
                                   @current-change="_GetFinanceLoanDetail"
                                   :current-page.sync="pagination.currentPage"
                                   :page-sizes="pagination.pagesizes"
                                   :page-size.sync="pagination.pagesize"
                                   layout="total, sizes, prev, pager, next, jumper"
                                   :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>

        <el-dialog :visible.sync="AddFinanceLoanShow" width="60%" top="10px" title="添加财务借款">
            <AddFinanceLoan :AllCategory="AllCategory"
                            :GetPayArray="GetPayArray"
                            :AllUsers="AllUsers"
                            :AllProject="AllProject"
                            :AllDepartment="AllDepartment"></AddFinanceLoan>
        </el-dialog>

        <el-dialog :visible.sync="UpFinanceLoanShow" v-if="UpFinanceLoanShow" width="60%" top="10px" title="修改财务借款">
            <UpFinanceLoan :AllCategory="AllCategory" :editPayee="editPayee" :FinanceLoanDB="FinanceLoanDB"
                           :GetPayArray="GetPayArray" :AllUsers="AllUsers" :AllProject="AllProject"
                           :AllDepartment="AllDepartment"></UpFinanceLoan>
        </el-dialog>

        <el-dialog :visible.sync="AddFinanceReimbursementShow" width="60%" top="10px" title="报销冲账">
            <AddFinanceReimbursement
                    :GetPayArray="GetPayArray"
                    :AllUsers="AllUsers"
                    :AllProject="AllProject"
                    :AllDepartment="AllDepartment"
                    :AllCategory="AllCategory"
                    :editPayee="editPayee"
                    :FinanceReimbursementDB="FinanceLoanDB"></AddFinanceReimbursement>
        </el-dialog>

        <el-dialog :visible.sync="FinanceLoanOrderShow" class="DialogTable" :fullscreen="true" title="借款单"
                   @close="FinanceLoanOrderClose">
            <FinanceLoanOrder :dataDB="FinanceLoanDB"
                              :TableTypeNumber="FinanceLoanDB.FinanceType"
                              :AllUsers="AllUsers"
                              :AllCustomer="AllCustomer"
                              :AllProject="AllProject"
                              :AllCategory="AllCategory"></FinanceLoanOrder>
        </el-dialog>
        <el-dialog :visible.sync="multiplexData.show" v-if="multiplexData.show" width="60%" top="10px"
                   :title="multiplexData.title">
            <FinanceLoanmultiplex :AllCategory="AllCategory"
                                  :GetPayArray="GetPayArray"
                                  :AllUsers="AllUsers" :editPayee="editPayee"
                                  :AllProject="AllProject" :FinanceLoanDB="multiplexData.data"
                                  />
        </el-dialog>

        <el-dialog :visible.sync="purChaseData.show" v-if-="purChaseData.show" title="采购单" width="100%" top="0">
            <PurChase :purChaseData="purChaseData.data"/>
        </el-dialog>
    </div>
</template>

<script>
    import * as publicjs from "/public/Scriput/Public"
    import AddFinanceLoan from "./AddFinanceLoan"
    import UpFinanceLoan from "./UpFinanceLoan"
    import AddFinanceReimbursement from './AddFinanceReimbursement'
    import FinanceLoanmultiplex from "./FinanceLoanmultiplex"
    //申请单
    import FinanceLoanOrder from "../../Report/FinanceLoanOrder";
    // 采购
    import PurChase from "./PurChase/PurChase"


    export default {
        name: "FinanceLoan",
        components: {
            'AddFinanceLoan': AddFinanceLoan,
            'UpFinanceLoan': UpFinanceLoan,
            'AddFinanceReimbursement': AddFinanceReimbursement,
            'FinanceLoanOrder': FinanceLoanOrder,
            "FinanceLoanmultiplex": FinanceLoanmultiplex,
            "PurChase":PurChase
        },
        data() {
            return {
                AddFinanceLoanShow: false,
                UpFinanceLoanShow: false,
                AddFinanceReimbursementShow: false,
                FinanceLoanOrderShow: false,
                AllUsers: [],
                AllDepartment: [],
                AllCustomer: [],
                AllProject: [],
                tableData: [],
                GetPayArray: [],
                AllCategory: [],
                //表格选中的数据
                SelectMore_lis: [],
                FinanceLoanDB: {
                    LoanNumber: '',
                    Project: '',
                    Borrower: '',
                    BorrowTime: '',
                },
                SelectProp: {
                    LoanNumber: '',
                    Project: '',
                    Borrower: '',
                    BorrowTime: '',
                },
                form: {
                    Start: '',
                    End: '',
                },
                editPayee: [],
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 0
                },
                // 复用数据
                multiplexData: {
                    show: false,
                    data: {},
                    title: ""
                },
                tablefinanceLoansShow: false,
                purChaseData: {
                    show: false,
                    data: []
                },
                tableButtonShow:false
            }
        },
        created() {
            this.AllDepartment = publicjs.GetAllDepartment(this.$http);
            this.AllProject = publicjs.GetAllProject(this.$http);
            this.AllUsers = publicjs.GetCompanyAllUsers(this.$http);
            this.AllCustomer = publicjs.GetAllCustomer(this.$http);
            this.GetPayArray = publicjs.GetPayArray(this.$http);
            this._GetFinanceLoanDetail();
            this.GetAllFinanceCategorytoarray();
        },
        methods: {
            ShowAddFinanceLoan() {
                //this.editPayee = [];
                //this.FinanceLoanDB = {};
                this.AddFinanceLoanShow = true;
            },

            _UserById(id) {
                for (var i = 0; i < this.AllUsers.length; i++) {
                    if (this.AllUsers[i].Id == id) {
                        return this.AllUsers[i].UserName;
                    }
                }
            },

            _GEtAllDepartmentById(scope) {
                for (var i = 0; i < this.AllDepartment.length; i++) {
                    if (this.AllDepartment[i].Id == scope.Department) {
                        return this.AllDepartment[i].DepartmentName;
                    }
                }
            },

            _ProjectById(d) {
                for (var i = 0; i < this.AllProject.length; i++) {
                    if (this.AllProject[i].Id == d.Project) {
                        return this.AllProject[i].ProjectName;
                    }
                }
            },
            doNewDateTotime(time) {
                if (time) {
                    return publicjs.formatDate(new Date(time), "yyyy-MM-dd hh:mm:ss")
                } else {
                    return "";
                }
            },
            _AllCustomerById(d) {
                if (d.PayeeType == 1) {
                    for (var i = 0; i < this.AllCustomer.length; i++) {
                        if (this.AllCustomer[i].Id == d.Payee) {
                            return this.AllCustomer[i].CustomerCompany;
                        }
                    }
                } else if (d.PayeeType == 2) {
                    for (var i = 0; i < this.AllUsers.length; i++) {
                        if (this.AllUsers[i].Id == d.Payee) {
                            return this.AllUsers[i].UserName;
                        }
                    }
                }
            },

            _GetInitState(d) {
                //为各个单据页面数据初始化时的状态列初始化
                var result = "";
                if (d.State == 0) {
                    result = "草稿";
                } else if (d.State == 1) {
                    result = "待审批";
                } else if (d.State == 2) {
                    if (d.IsRefuse) {
                        result = "<span style='color:#FF5722'>第一级已拒绝</span>";
                    } else {
                        result = "<span style='color:#1E90FF'>第一级已同意</span>";
                    }
                } else if (d.State == 4) {
                    if (d.IsRefuse) {
                        result = "<span style='color:#FF5722'>第二级已拒绝</span>";
                    } else {
                        result = "<span style='color:#7CFC00'>第二级已同意</span>";
                    }
                } else if (d.State == 8) {
                    if (d.IsRefuse) {
                        result = "<span style='color:#FF5722'>第三级已拒绝</span>";
                    } else {
                        result = "<span style='color:#5e09f5'>第三级已同意</span>";
                    }
                } else if (d.State == 16) {
                    if (d.IsRefuse) {
                        result = "<span style='color:#FF5722'>第四级已拒绝</span>";
                    } else {
                        result = "<span style='color:#009688'>第四级已同意</span>";
                    }
                } else if (d.State == 32) {
                    if (d.IsRefuse) {
                        result = "<span style='color:#FF5722'>第五级已拒绝</span>";
                    } else {
                        result = "<span style='color:#108ae4'>第五级已同意</span>";
                    }
                } else if (d.State == 64) {
                    if (d.IsRefuse) {
                        result = "<span style='color:#FF5722'>第六级已拒绝</span>";
                    } else {
                        result = "<span style='color:#fbb80a'>出六已同意</span>";
                    }
                }
                if(d.IsAuditEnd){
                    result += "<br/><span style='color:green'>审批完成</span>";
                }
                return result;
            },
            _indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },


            GetAllFinanceCategorytoarray() {
                this.$http.get("/PublicMethodApi/GetAllFinanceCategory").then(x => {
                    for (var i = 0; i < x.data.length; i++) {
                        this.AllCategory.push(x.data[i])
                    }
                }).catch(() => {
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //初始化表格数据加载
            _GetFinanceLoanDetail() {
                const Loading = this.openLoading();
                this.$http.post("/Finance/GetFinanceLoanDetail", {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    form: this.form,
                    FinanceLoanDB: this.SelectProp,
                }).then(x => {
                    Loading.close();
                    this.pagination.total = x.data.count
                    this.tableData = x.data.data;
                }).catch(() => {
                    Loading.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //重置查询
            _GetFinanceLoanDetail_Refresh() {
                this.$refs["queryform_FinanceLoan"].resetFields()
                this.form.Start = ''
                this.form.End = ''
                this.form.Payee = []
                this.form.NoChecked = false
                this.form.StartMoney = ''
                this.form.EndMoney = ''
                this.SelectProp.IsRefuse = false
                this._GetFinanceLoanDetail()
            },

            //借款单
            order(obj) {
                let ObjJsonparse = JSON.parse(JSON.stringify(obj))
                this.$http.get("/Finance/FinanceLoanOrderbit?FinanceLoanNumber=" + obj.LoanNumber).then((list) => {
                    var result = list.data;
                    if (result.mes.IsSuccess) {
                        this.FinanceLoanDB = ObjJsonparse;
                        this.FinanceLoanOrderShow = true;
                    } else {
                        this.$message.error(result.mes.Msg)
                    }
                });
            },
            // 再付
            multiplex(row) {
                this.$http.post("/Finance/ValidateLoanIsAuditFinish", {data: row.LoanNumber}).then((result) => {
                    if (result.data.IsSuccess) {
                        this.multiplexData = {
                            data: {
                                LoanNumber: row.LoanNumber,
                                FinanceType: row.FinanceType,
                                Project: row.Project,
                                Department: row.Department,
                                Borrower: row.Borrower,
                                IsRefuse: row.IsRefuse,
                                PayeeType: row.PayeeType,
                                Money: row.Money,
                                Remark: row.Remark,
                                PurchaseNumbers: row.PurchaseNumbers,
                                HasInvoice:row.HasInvoice,
                                TransferMethod: row.TransferMethod,
                                PrId: row.PrId ? row.PrId : row.Id,
                            },
                            show: true,
                            title: "复用单：" + row.LoanNumber
                        }
                        this.editPayee = [];
                        this.editPayee.push(row.PayeeType + '')
                        this.editPayee.push(row.Payee + '')
                    } else {
                        this.$message.error(result.data.Msg)
                    }
                });
            },
            // 采购单信息
            onPurChase(row) {
                const Loading = this.openLoading();
                this.$http.post("Finance/GetPurchase", {loanNumber: row.LoanNumber}).then((res) => {
                    if (res.data.length) {
                        this.purChaseData = {
                            show: true,
                            data: res.data
                        }
                    } else {
                        this.$message.info('没有采购单数据!')
                    }
                    Loading.close();
                }).catch(() => {
                    Loading.close();
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //借款单关闭
            FinanceLoanOrderClose() {
                this._GetFinanceLoanDetail();
            },
            //修改
            edit(obj) {
                this.$http.post("/Finance/ValidateLoanState", {data: obj.LoanNumber}).then((result) => {
                    if (result.data.IsSuccess) {
                        this.editPayee = [];
                        this.editPayee.push(obj.PayeeType + '')
                        this.editPayee.push(obj.Payee + '')
                        var objDB = JSON.parse(JSON.stringify(obj))
                        this.FinanceLoanDB = objDB;
                        this.UpFinanceLoanShow = true;
                    } else {
                        this.$message.error(result.data.Msg)
                    }
                });
            },
            //冲账
            rush(obj) {
                this.$http.get('Finance/AddFinanceReimbursementBit?LoanNumber=' + obj.LoanNumber).then(res => {
                    if (res.data.mes.IsSuccess) {
                        //开启数据页面
                        this.editPayee = [];
                        this.editPayee.push(obj.PayeeType + '')
                        this.editPayee.push(obj.Payee + '')
                        this.FinanceLoanDB = res.data.obj;
                        this.AddFinanceReimbursementShow = true;
                    } else {
                        this.$message.error(res.data.mes.Msg)
                    }
                });
            },
            //删除
            del(obj) {
                this.SelectMore_lis = [];
                this.SelectMore_lis.push(obj);
                this.DelArray();
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //批量删除
            DelArray() {
                let arrDel = []
                if (this.SelectMore_lis.length > 0) {
                    this.$confirm('你确定要删除吗?', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消'
                    }).then(() => {
                        this.SelectMore_lis.forEach(item => {
                            arrDel.push(item.Id)
                        })
                        let Loading = this.openLoading();
                        this.$http.post('Finance/DeleteFinanceLoan', arrDel).then(res => {
                            Loading.close();
                            if (res.data.IsSuccess) {
                                this.$message.success('删除成功!')
                                this.SelectMore_lis = []
                                this._GetFinanceLoanDetail()
                            } else {
                                this.$message.error(res.data.Msg)
                            }
                        }).catch(() => {
                            Loading.close();
                            this.$message.info('服务器异常,请联系管理员!')
                        })
                    })
                } else {
                    this.$message.warning('没有选中的数据!')
                }
            },

        },


    }
</script>
<style scoped>

    .big_box {
        padding: 15px;
    }

    .row-bg {
        padding: 10px 0;
        background-color: #f9fafc;
    }

    .buttongroup {
        margin-right: 5px !important;
        border-radius: 4px !important;
    }

    .DialogTable {
        position: fixed;

    }

    .elformitem {
        margin-bottom: 0px !important;
    }

    .tablefinanceLoans {
        padding: 5px 0;
        cursor: pointer;
    }

    .tablefinanceLoans:hover {
        border-bottom: 1px #5FB878 solid;
    }
</style>
